#!/usr/bin/php

<?php
/*
ugy fogunk mukodni, hogy 
1. vegtelen ciklusban
2. nezzuk a plugins.conf -ot
3. ami ott van azt ciklusonkent lefuttatjuk

*/
//nem masodperc alapon hanem ciklus alapon mukodunk
// master detials
$master_host = '192.168.1.102';
$master_port = '7801';

$plugin_dir = './plugins';
$hostname = explode("\n", `hostname -s`);
$hostname=$hostname[0];

$loop_init = 9;

while (true) {
$loop_init += 1;

include 'plugins.conf.php';

// check what we should run for this host..
foreach ( $hosts[$hostname] as $group ) {
    foreach ( $group_checks[$group] as $check ) {
	$plugin_interval = $plugins[$check];
	if ( $loop_init % $plugin_interval == 0 ) {
	    // now we can open the processes and report.. 
	    $handle = popen($plugin_dir."/".$check." 2>&1", 'r');
	    $read = fread($handle, 2096);
	    //$read will contain the result from the plugin 
	    //so lets report now
	    // al report should go to the master defined above
	    // all report should follow a schema like this:
	    //$group,$host,$check,$value   
	    $out = "str ".$group.",".$hostname.",".$check.",".$read;
	    pclose($handle);
	    $listener = fsockopen ( $master_host , $master_port, $errno, $errstr, 5);
	    if (!$listener) {
	        echo "$errstr ($errno)<br />\n";
	        } else {
		fwrite($listener, $out);
		    while (!feof($listener)) {
			echo fgets($listener, 128);
		    }
	    }
	}
    }

}


sleep (1);
}

?>